<template>
  <div>
    <!-- 面包屑导航区域 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/Welcome' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>订单管理</el-breadcrumb-item>
      <el-breadcrumb-item>订单列表</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 卡片视图区域 -->
    <el-card>
      <el-row :gutter="20">
        <el-col :span="8">
          <el-input v-model="queryInfo.query" placeholder="请输入内容" clearable @clear="getOrderList">
          <el-button slot="append" icon="el-icon-search" @click="getOrderList"></el-button>
        </el-input>
        </el-col>
      </el-row>
      <!-- 订单列表的表格区域 -->
       <el-table :data="orderList" border stripe>
        <!-- 索引列 -->
        <el-table-column type="index">
        </el-table-column>
        <el-table-column label="订单编号" prop="order_number" width="300px">
        </el-table-column>
         <el-table-column label="订单价格" prop="order_price" width="240px">
        </el-table-column>
        <el-table-column label="是否付款" prop="order_pay" width="240px">
          <template slot-scope="scope">
            <el-tag type="danger" v-if="scope.row.order_pay === '0'">未付款</el-tag>
            <el-tag type="success" v-else>已付款</el-tag>
          </template>
        </el-table-column>
         <el-table-column label="是否发货" prop="is_send" width="240px">
        </el-table-column>
        <el-table-column label="下单时间" prop="create_time" width="320px">
          <template slot-scope="scope">
            <!-- 使用自定义格式化时间的全局过滤器dateFormat，将时间戳形式的时间转换成yyyy-mm-dd hh:mm:ss形式的字符串时间 -->
            {{ scope.row.create_time | dateFormat }}
          </template>
        </el-table-column>
        <el-table-column label="操作" width="130px">
          <template slot-scope="scope">
            <el-button type="primary" size="mini" icon="el-icon-edit" @click="showEditOrder(scope.row)"></el-button>
            <el-button type="success" size="mini" icon="el-icon-location" @click="showProgressBox(scope.row)"></el-button>
          </template>
        </el-table-column>
      </el-table>
       <!-- 商品列表的分页导航区域 -->
       <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="queryInfo.pagenum"
        :page-sizes="[5, 10, 15, 20]"
        :page-size="queryInfo.pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total" background>
      </el-pagination>
    </el-card>
    <!-- 修改地址的对话框 -->
    <el-dialog
      title="修改地址"
      :visible.sync="addrDialogVisible"
      width="50%" @close="addressDialogClosed">
      <el-form :model="addrForm" :rules="addrFormRules" ref="addrFormRef" label-width="100px">
        <el-form-item label="省市区/县" prop="city">
           <el-cascader
            v-model="addrForm.city"
            :options="cityData"
            :props="addrProps"
            @change="handleChange">
           </el-cascader>
        </el-form-item>
        <el-form-item label="详细地址" prop="detailAddress">
          <el-input v-model="addrForm.detailAddress"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="addrDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="addrDialogVisible = false">确 定</el-button>
      </span>
    </el-dialog>
    <!-- 展示物流进度的对话框 -->
    <el-dialog title="物流进度" :visible.sync="progressDialogVisible" width="50%">
      <!-- 时间线 -->
      <el-timeline>
        <el-timeline-item
          v-for="(activity, index) in progressInfoList"
          :key="index"
          :timestamp="activity.time">
          {{activity.context}}
        </el-timeline-item>
      </el-timeline>
    </el-dialog>
  </div>
</template>
<script>
import cityData from './citydata.js'

export default {
  data () {
    return {
      // 查询参数对象
      queryInfo: {
        // 查询参数
        query: '',
        // 当前页码
        pagenum: 1,
        // 每页显示条数
        pagesize: 10
      },
      // 获取到的订单列表数组
      orderList: [],
      // 订单列表的总商品数目
      total: 0,
      // 控制编辑商品的修改地址的对话框的显示与隐藏
      addrDialogVisible: false,
      // 地址的表单对象
      addrForm: {
        city: [],
        // 详细地址
        detailAddress: ''
      },
      // 地址表单的验证规则对象
      addrFormRules: {
        city: [
          { required: true, message: '请选择省市区/县', trigger: 'blur' }
        ],
        detailAdd: [
          { required: true, message: '请输入详细地址', trigger: 'blur' }
        ]
      },
      // 省市区/县的数据
      // 如果键值名称相同，可以简写
      cityData,
      // 级联选择框的Props项
      addrProps: {
        value: 'value',
        label: 'label',
        expandTrigger: 'hover'
      },
      // 控制物流进度对话框的显示与隐藏
      progressDialogVisible: false,
      // 物流快递进度信息数组
      progressInfoList: []
    }
  },
  created () {
    // 获取订单数据列表
    this.getOrderList()
  },
  methods: {
    // 获取订单列表数据
    async getOrderList () {
      const { data: res } = await this.$http.get('orders', {
        params: this.queryInfo
      })
      if (res.meta.status !== 200) {
        return this.$message.error('获取订单列表失败！')
      }
      this.$message.success('获取订单列表成功！')
      console.log(res)
      this.orderList = res.data.goods
      this.total = res.data.total
    },
    // pageSize 改变时会触发
    handleSizeChange (newPageSize) {
      // console.log(`每页${newPageSize}条`)
      this.queryInfo.pagesize = newPageSize
      // 重新获取订单列表，刷新列表
      this.getOrderList()
    },
    // currentPage 改变时会触发
    handleCurrentChange (curenetPage) {
      // console.log(`当前页：${curenetPage}`)
      this.queryInfo.pagenum = curenetPage
      // 重新刷新订单列表
      this.getOrderList()
    },
    // 展示修改地址的对话框
    showEditOrder (row) {
      this.addrDialogVisible = true
    },
    locateOrderById (orderId) {
    },
    // 级联选择框 当选中节点变化时触发，回调参数为选中节点的值
    handleChange (selectedKeys) {
      console.log(selectedKeys)
    },
    // 修改地址对话框的关闭事件处理函数
    addressDialogClosed () {
      this.$refs.addrFormRef.resetFields()
    },
    // 显示物流进度的对话框
    async showProgressBox (row) {
      console.log('打印当前行的数据信息')
      console.log(row)
      const { data: res } = await this.$http.get('/kuaidi/1106975712662')
      if (res.meta.status !== 200) {
        return this.$message.error('获取物流信息失败！')
      }
      this.$message.success('获取物流信息成功！')
      this.progressInfoList = res.data
      console.log(this.progressInfoList)
      this.progressDialogVisible = true
    }
  }
}
</script>
<style lang="less" scoped>
.el-cascader {
  width: 100%;
}
</style>
